﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TheoriesBraun.BLL;
using TheoriesBraun.DAL;

namespace TheoriesBraun
{
    public partial class Login : Form
    {
        Validates check = null;
        Theories mainForm = null;
        Management management = null;

        public Login()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            mainForm = new Theories();
            mainForm.Visible = true;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            check = new Validates();
            management = new Management();
            string uname = txtAccount.Text;
            string pass = txtPassword.Text;

            UserBLL userBLL = new UserBLL();
            userBLL.Account = uname;
            userBLL.Password = pass;

            if (uname.Equals("") == false || pass.Equals("") == false)
            {
                if (pass.Length >= 4)
                {
                    if (check.IsAlphaNumeric(uname) && check.IsAlphaNumeric(pass))
                    {
                        UserDAL userDAL = new UserDAL();
                        if (userDAL.Login(userBLL) == true)
                        {
                            management.Show();
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Your Username or Password entered is wrong!");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Your Username or Password entered is invalid!");
                    }
                }
                else
                {
                    MessageBox.Show("Password have more than four characters!");
                }
            }
            else
            {
                MessageBox.Show("Username and Password is not empty!");
            }
        }

        private void Login_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, new EventArgs());
            }
        }
    }
}
